<?php
/**
 * Created by PhpStorm.
 * User: shiyang
 * Date: 2018/12/17
 * Time: 21:34
 */

namespace App\Http\Controllers\Art;


use App\Http\Controllers\Controller;
use App\Http\Requests\ArticleCategoryRequest;
use App\Libraries\DataSource;
use App\Models\ArticleCategory;
use Response;

class CategoryController extends Controller
{
    public function index()
    {
        $this->authorize('art.category');
        return view('art.category.index');
    }

    public function table()
    {
        $categories = ArticleCategory::orderBy('parent_id', 'asc')->orderBy('sort', 'desc')->get();

        $categories->transform(function ($category){
            return [
                'id' => $category->id,
                'parent_chain' => $category->parent_chain ?: '-',
                'name' => $category->name,
                'sort' => $category->sort,
                'state' => $category->state,
                'state_name' => ArticleCategory::STATES[$category->state],
                'created_at' => $category->created_at->toDateTimeString(),
            ];
        });

        $ds = new DataSource();
        $ds->data = $categories;

        return Response::json($ds);
    }

    public function create()
    {
        $this->authorize('art.category');
        $parents = ArticleCategory::getChains();
        return view('art.category.create', compact('parents'));
    }

    public function store(ArticleCategoryRequest $request)
    {
        $this->authorize('art.category');
        $input = $request->all();
        if($input['parent_id'] > 0) {
            $input['parent_chain'] = ArticleCategory::getParentChain($input['parent_id']);
        }
        ArticleCategory::create($input);

        \Session::flash('flash_success', '添加成功');
        return redirect('art/category');
    }

    public function edit($id)
    {
        $this->authorize('art.category');
        $parents = ArticleCategory::getChains();
        $category = ArticleCategory::find($id);
        if ($category == null) {
            \Session::flash('flash_warning', '无此记录');
            return redirect()->back();
        }

        return view('art.category.edit', compact('category', 'parents'));
    }

    public function update($id, ArticleCategoryRequest $request)
    {
        $this->authorize('art.category');
        $input = $request->all();
        $category = ArticleCategory::find($id);
        if($input['parent_id'] > 0) {
            $input['parent_chain'] = ArticleCategory::getParentChain($input['parent_id']);
        }
        $category->update($input);

        \Session::flash('flash_success', '修改成功!');
        return redirect('art/category');
    }

    public function destroy($id)
    {
        $this->authorize('art.category');
        $category = ArticleCategory::query()->find($id);
        if (! $category) {
            return response()->json([
                'code' => 404,
                'message' => '未找到记录'
            ]);
        }
        $flag = $category->delete();
        if($flag) {
            return response()->json([
                'code' => 200,
                'message' => 'Success'
            ]);
        }
        return response()->json([
            'code' => 500,
            'message' => '服务器异常'
        ]);
    }
}
